﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FormLogin.cs" company="">
//   
// </copyright>
// <summary>
//   The form login.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace NetworkSupervisor
{
    using System;
    using System.Linq;
    using System.Windows.Forms;

    using DevExpress.Xpo;
    using DevExpress.XtraEditors;

    using NetworkSupervisor.Model.NetworkSupervisor;

    /// <summary>
    /// The form login.
    /// </summary>
    public partial class FormLogin : XtraForm
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="FormLogin"/> class.
        /// </summary>
        public FormLogin()
        {
            this.InitializeComponent();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets a value indicating whether is login.
        /// </summary>
        public bool IsLogin { get; set; }

        #endregion

        #region Properties

        /// <summary>Gets or sets a value indicating whether is close.</summary>
        protected bool IsClose { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The lock it.
        /// </summary>
        public void LockIt()
        {
            this.btnExit.Enabled = false;
            this.txtPassword.Text = string.Empty;
        }

        #endregion

        #region Methods

        /// <summary>The form login_ form closing.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void FormLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = !this.IsClose;
        }

        /// <summary>The form login_ load.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void FormLogin_Load(object sender, EventArgs e)
        {
            this.Icon = Properties.Resources.app;
            this.BeginInvoke(new MethodInvoker(() => this.InitDatabase()));
        }

        /// <summary>The form login_ shown.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void FormLogin_Shown(object sender, EventArgs e)
        {
            this.BeginInvoke(new MethodInvoker(() => Program.gFormMain = new FormMain()));

            this.txtPassword.Focus();
        }

        /// <summary>The init database.</summary>
        private void InitDatabase()
        {
            using (Session session = new Session())
            {
                XPCollection<ta用户> collection = new XPCollection<ta用户>(session);
                if (collection.Count < 1)
                {
                    ta用户 item = new ta用户(session);
                    item.用户名 = "admin";
                    item.密码 = "admin";
                    item.Save();
                }

                XPCollection<ta类别> collection2 = new XPCollection<ta类别>(session);
                if (collection2.Count < 1)
                {
                    string[] strings = new[] { "综合", "新闻", "博客", "论坛", "微博", "关注", "敏感" };
                    foreach (string str in strings)
                    {
                        ta类别 item = new ta类别(session);
                        item.类别 = str;
                        item.Save();
                    }
                }
                {
                    ta搜索结果 item = new ta搜索结果(session);
                    item.Save();
                    item.Delete();
                }
            }
        }

        /// <summary>The btn exit_ click.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.IsClose = true;
            this.Close();
        }

        /// <summary>The btn login_ click.</summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            string user = this.txtUser.Text;
            string password = this.txtPassword.Text;
            XPCollection<ta用户> collection = new XPCollection<ta用户>(this.session1);
            if (collection.Any(o => o.用户名 == user && o.密码 == password))
            {
                this.Hide();
                Program.gFormMain.Show();
                Program.Context.MainForm = Program.gFormMain;
            }
            else
            {
                XtraMessageBox.Show("密码错误");
            }
        }

        #endregion
    }
}